﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;
using MoneyManagerNET.Presentation.Infrastructure.Binding;

namespace MoneyManagerNET.Presentation
{
    public static class BindingExtensions
    {

        /*
         * allows to use compile time validation for property names and intellisense
         */
        public static void OnChange<TProp>(this INotifyable src, Expression<Func<TProp>> property)
        {
            var propName = Expression2PropertyName<TProp>(property);

            src.OnChange(propName);
        }

        public static string Expression2PropertyName<TProp>(Expression<Func<TProp>> property)
        {
            var lambda = (LambdaExpression)property;

            MemberExpression memExp = null;
            if (lambda.Body is UnaryExpression)
            {
                var uExp = lambda.Body as UnaryExpression;
                memExp = uExp.Operand as MemberExpression;
            }
            else memExp = lambda.Body as MemberExpression;
            return memExp.Member.Name;
        }
    }
}
